<template>
	<view class="content b-t">
		<view class="main-wrap" :class="{ show: loaded }">
			<s-pull-scroll ref="pullScroll" :pullDown="pullDown" :enablePullDown="false" :pullUp="loadData" :back-top="true" :footerHeight="65">
				<view class="list row between" @tap.stop="checkAddress(item)" v-for="(item, index) in dataList" :key="index" style="align-items: center;">
					<view style="width:90%;">
						<view class="wrapper">
							<view class="row">
								<view class="name">{{ item.name }}</view>
								<view class="mobile">{{ item.phone }}</view>
							</view>
							<view class="row" style="align-items: flex-start;padding-top: 20upx;">
								<view class="default" v-if="item.isDefault == 1">默认</view>
								<view class="address fill">{{ item.province }}{{ item.city }}{{ item.county }}{{ item.detail }}</view>
							</view>
						</view>
					</view>
					<view style="width:10%;" class="row">
						<view class="fill row center"><u-icon name="/static/img/bj.png" size="40" @tap.stop="editItem(item)"></u-icon></view>
					</view>
				</view>
			</s-pull-scroll>
			<view class="footer-btn row center"><view class="add-btn" @tap="navTo(`/pages/address/newAddAdress`)" hover-class="hover-opacity">添加地址</view></view>
		</view>

		<mix-loading v-if="isLoading"></mix-loading>
	</view>
</template>

<script>
export default {
	data() {
		return {
			source: 0,
			dataList: []
		};
	},
	onLoad(options) {
		console.log(options.source);
		this.source = options.source;
		this.refresh();
		uni.$on('change_address', parameter => {
			console.log(parameter);
			this.refresh();
			if (parameter) {
				this.perData(parameter);
			}
		});
	},
	methods: {
		pullDown(pullScroll) {
			setTimeout(() => {
				this.loadData(pullScroll);
			}, 200);
		},
		async loadData(pullScroll) {
			const res = await this.$request('address/list', { uid: uni.getStorageSync('uid') }, { showLoading: true });
			if (pullScroll.page == 1) {
				this.dataList = [];
			}
			if (res.result == 0) {
				this.dataList = res.dataList;
				pullScroll.endByPage(1, res.dataList);
			} else {
				this.$util.msg(res.resultNote);
			}
		},
		editItem(item){
			this.navTo(`/pages/address/newAddAdress?id=${item.id}&item=${JSON.stringify(item)}`);
		},
		//选择地址
		checkAddress(item) {
			if (this.source == 1) {
				this.$util.prePage().setAddress(item);
				this.back();
			}
		},
		perData(parameter) {
			if (this.source == 1) {
				if (parameter.id == this.$util.prePage().address.id) {
					if(parameter.type=='del'){
						this.$util.prePage().setAddress();
					}else{
						let array=this.dataList.find(item=>{return item.id==parameter.id});
						this.$util.prePage().setAddress(array[0]);
					}
				}
			}
		}
	}
};
</script>
<style>
page {
	background: #f6f6f6;
}
</style>

<style lang="scss" scoped>
.tit {
	font-size: 21upx;
	color: #666666;
}

.list {
	margin-bottom: 10upx;
	padding: 20upx;
	background: #fff;
}
.default {
	padding: 4upx 8upx;
	// height: 36upx;
	text-align: center;
	// line-height: 36upx;
	background: #2781a7;
	border-radius: 6upx;
	font-size: 20upx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #f6f6f6;
	margin-right: 20upx;
}
.address {
	font-size: 24upx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #666666;
	line-height: 43upx;
}
.name {
	font-size: 26upx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #333333;
}

.mobile {
	font-size: 24upx;
	font-family: DIN;
	font-weight: 500;
	color: #999999;
	margin-left: 30upx;
}
.footer-btn {
	position: fixed;
	bottom: 0;
	left: 0;
	z-index: 99;
	width: 100%;
	height: 120upx;
}
.add-btn {
	width: 700upx;
	line-height: 80upx;
	text-align: center;
	background: #2781a7;
	color: #ffffff;
	font-size: 30upx;
	border-radius: 39upx;
	margin: 0 auto;
}
</style>
